﻿namespace FBlog.UnitTests.WhenUsingBlog

open System
open Xunit

open FBlog.Core
open FBlog.UnitTests.Common
open FBlog.UnitTests.Common.BlogTester

module TestPublishingOfBlogPost =

    let testEnvironment = new EnvironmentMock(testAuthor, testPublishTime)
    let testBlogPostRep = new BlogPostRepositoryMock()

    let createBlog() = createTestBlog(testEnvironment, testBlogPostRep )

    [<Fact>]
    let totalPostCountOnEmptyBlogIsZero() =
        
        let blog = createBlog()

        Assert.Equal(0, blog.GetTotalPostCount())

    [<Fact>]
    let authorCanPublishPost() =
        
        let blog = createBlog()
        publishTestPos(blog) |> ignore

        let count = blog.GetTotalPostCount()

        Assert.Equal(1, count)

    [<Fact>]
    let postIdWillBeSetOnPublish() =
        let blog = createBlog()

        let post = publishTestPos(blog)

        Assert.NotEqual(Guid.Empty, post.Id)

    [<Fact>]
    let publishWillInsertThePostIntoTheBlogPostRepository() =
        let blog = createBlog()
        let blogId = publishTestPos(blog).Id

        let post = loadPostFromRepository(testBlogPostRep, blogId)
        assertPostData(post, testPublishTime)

    [<Fact>]
    let publishInfoWillBeSetOnPublish() =
        let blog = createBlog()

        let post = publishTestPos(blog)

        assertTestPublishInfo(post.PublishInfo, testPublishTime)

    [<Fact>]
    let republishWillSetChangeTimeNotPublishTimeOnPublishInfo() =
        let blog = createBlog()
        let post = publishTestPos(blog)
        let updateTime = testPublishTime.AddMinutes(10.0)

        testEnvironment.SetTime(updateTime)
        blog.Publish(post)

        assertTestPublishInfo(post.PublishInfo, updateTime)        